<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList1" @findClicked="findData" />
    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <div style="display: flex">
        <div style="width: 45%">
          <h1>{{ $t(`Print.DefinitionShoeUpperInventoryDepartment`) }}</h1>
          <Table
            :isLoading="isLoading"
            @currentRow="currentRowList"
            :tableHeader="tableHeader"
            :tableData="VampToTheWarehouseDepartmentMethodsTableDateLsit"
            :TableHeight="100"
            :outTableHeight="60"
          />
        </div>
        <div style="width: 11%; margin-left: 10px">
          <h1>{{ $t(`Print.ABranchGroupDefinition`) }}</h1>
          <Table
            :isLoading="isLoading1"
            @confirmChange="confirmChange1"
            @currentRow="currentRowList1"
            :tableHeader="tableHeader1"
            :tableData="BranchGroupDefinitionMethodsTableDateList"
            :TableHeight="100"
            :outTableHeight="60"
          />
        </div>
      </div>
      <PageModule :PageData="{ pageSizeShow: true }" />
    </el-card>

    <!-- 增加鞋面缴库部门定义 -->
    <el-dialog v-model="dialogVisible" :title="$t(`Print.AddTheUpperDepotDepartmentDefinition`)">
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList2" @findClicked="findData1" @Launchform="Launchform" />
    </el-dialog>

    <!-- 添加组别定义 -->
    <el-dialog v-model="dialogVisible1" :title="$t(`Print.AddTheUpperDepotDepartmentDefinition`)">
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList3" @findClicked="findData2" @Launchform="Launchform2" />
    </el-dialog>

    <!-- 修改鞋面缴库部门定义 -->
    <el-dialog v-model="dialogVisible2" :title="$t(`Print.AddTheUpperDepotDepartmentDefinition`)">
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList5" @findClicked="findData3" @Launchform="Launchform3" />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'DefinitionShoeUpperInventoryDepartment'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref, onMounted } from 'vue'
import {
  DepartmentDefinitionModification,
  CategoryGroupDefinitionAdded,
  VampDepositAdded,
  BranchGroupDefinitionDeleted,
  DeleteTheTreasuryDepartment,
  BranchGroupDefinition,
  VampToTheWarehouseDepartment
} from '@/api/WarehouseDepartment/FactoryStorage/DefinitionShoeUpperInventoryDepartment'
import type {
  DepartmentDefinitionModificationParms,
  CategoryGroupDefinitionAddedParms,
  VampDepositAddedParms,
  VampToTheWarehouseDepartmentInterface
} from '@/api/WarehouseDepartment/FactoryStorage/DefinitionShoeUpperInventoryDepartment'
import { ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus'

const isLoading = ref(false)

const isLoading1 = ref(false)
const formList1 = ref([
  {
    type: 'button',
    checkboxLabel: '添加鞋面缴库部门定义'
  },
  {
    type: 'button',
    checkboxLabel: '添加分厂组别定义'
  },
  {
    type: 'button',
    checkboxLabel: '删除鞋面缴库部门定义'
  },
  {
    type: 'button',
    checkboxLabel: '删除分厂组别定义'
  },
  {
    type: 'button',
    checkboxLabel: '修改鞋面缴库部门定义'
  },
  {
    type: 'button',
    checkboxLabel: '修改分厂组别定义'
  }
])

const formList2 = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '部门分类',
    selectLabel: '',
    selectOptions: [
      { optionKey: 0, optionValue: '本厂', optionLabel: '本厂' },
      { optionKey: 1, optionValue: '分厂', optionLabel: '分厂' },
      { optionKey: 2, optionValue: '外包', optionLabel: '外包' },
      { optionKey: 3, optionValue: '外发', optionLabel: '外发' },
      { optionKey: 4, optionValue: '携带', optionLabel: '携带' }
    ]
  },
  {
    type: 'input',
    inputPlaceholder: '缴库部门',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '组别代码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '组别名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '人数',
    inputValue: ''
  },
  {
    type: 'checkbox',
    checkboxLabel: '外包厂商'
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  }
])

const formList3 = ref([
  {
    type: 'input',
    inputPlaceholder: '组别代码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '组别名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  }
])

const formList5 = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '部门分类',
    selectLabel: '',
    selectOptions: [
      { optionKey: 0, optionValue: '本厂', optionLabel: '本厂' },
      { optionKey: 1, optionValue: '分厂', optionLabel: '分厂' },
      { optionKey: 2, optionValue: '外包', optionLabel: '外包' },
      { optionKey: 3, optionValue: '外发', optionLabel: '外发' },
      { optionKey: 4, optionValue: '携带', optionLabel: '携带' }
    ]
  },
  {
    type: 'input',
    inputPlaceholder: '缴库部门',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '组别代码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '组别名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '人数',
    inputValue: ''
  },
  {
    type: 'checkbox',
    checkboxLabel: '外包厂商',
    checked: false
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  }
])

const findData = (row: any) => {
  if (row === '删除鞋面缴库部门定义') {
    if (DeleteTheTreasuryDepartmentMethodsParms.value === '') {
      JMessage.warning('请选择需要删除的数据')
    } else {
      handleCloseMateriaLKlc5()
    }
  }
  if (row === '删除分厂组别定义') {
    if (BranchGroupDefinitionDeletedMethodsParms.value === '') {
      JMessage.warning('请选择需要删除的数据')
    } else {
      handleCloseMateriaLKlc()
    }
  }
  if (row === '添加鞋面缴库部门定义') {
    dialogVisible.value = true
  }
  if (row === '添加分厂组别定义') {
    dialogVisible1.value = true
  }
  if (row === '修改鞋面缴库部门定义') {
    if (VampToTheWarehouseDepartmentListParms.value === undefined) {
      JMessage.warning('请选择需要修改的数据')
    } else {
      dialogVisible2.value = true
    }
  }
}

const findData1 = (row: any) => {
  if (row === '保存') {
    if (
      VampDepositAddedMethodsParms.value.persons === '' ||
      VampDepositAddedMethodsParms.value.groupDept === '' ||
      VampDepositAddedMethodsParms.value.factoryDept === '' ||
      VampDepositAddedMethodsParms.value.collectDeptNew === '' ||
      VampDepositAddedMethodsParms.value.collectName === ''
    ) {
      JMessage.warning('输入数据不完善请检查')
    } else {
      VampDepositAddedMethods()
    }
  }
}

const clearFormList2 = () => {
  formList2.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'selectValue') {
      item.selectValue = ''
    }
  })
}

const Launchform = (row: any) => {
  VampDepositAddedMethodsParms.value.groupDept = row[0].selectValue
  VampDepositAddedMethodsParms.value.factoryDept = row[1].inputValue
  VampDepositAddedMethodsParms.value.collectDeptNew = row[2].inputValue
  VampDepositAddedMethodsParms.value.collectName = row[3].inputValue
  VampDepositAddedMethodsParms.value.persons = row[4].inputValue
  VampDepositAddedMethodsParms.value.vendorFlag = row[5].checked === true ? 'Y' : ''
}

const dialogVisible2 = ref(false)

const updateList = ref({
  collectDept: '',
  deptName: '',
  deptNo: '',
  deptNoNew: '',
  factoryDept: ''
})

const confirmChange1 = async (row: any) => {
  updateList.value.deptNoNew = row.deptNo
  updateList.value.deptName = row.deptName
  const result = await LoginWork.put<any, any>(`/pcFactorStockQuery/stcColDetailUpdate`, updateList.value)
  if (result.code === 200) {
    BranchGroupDefinitionMethods()
    JMessage.success(result.message)
  } else {
    BranchGroupDefinitionMethods()
    JMessage.error(result.message)
  }
}

// 修改
const VampToTheWarehouseDepartmentListParms = ref()

const findData3 = (row: any) => {
  if (row === '保存') {
    DepartmentDefinitionModificationMethods()
  }
}

const Launchform3 = (row: any) => {
  DepartmentDefinitionModificationParms.value.groupDept = row[0].selectValue
  DepartmentDefinitionModificationParms.value.factoryDept = row[1].inputValue
  DepartmentDefinitionModificationParms.value.collectDeptNew = row[2].inputValue
  DepartmentDefinitionModificationParms.value.collectName = row[3].inputValue
  DepartmentDefinitionModificationParms.value.persons = row[4].inputValue
  DepartmentDefinitionModificationParms.value.vendorFlag = row[5].checked === true ? 'Y' : ''
}

const DepartmentDefinitionModificationParms = ref<DepartmentDefinitionModificationParms>({
  collectDept: '',
  collectDeptNew: '',
  collectName: '',
  factoryDept: '',
  groupDept: '',
  persons: '',
  vendorFlag: ''
})

const DepartmentDefinitionModificationMethods = async () => {
  const result = await DepartmentDefinitionModification(DepartmentDefinitionModificationParms.value)
  if (result.code === 200) {
    VampToTheWarehouseDepartmentMethods()
    dialogVisible2.value = false
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 添加组别定义
const dialogVisible1 = ref(false)

const Launchform2 = (row: any) => {
  CategoryGroupDefinitionAddedMethodsParms.value.deptName = row[1].inputValue
  CategoryGroupDefinitionAddedMethodsParms.value.deptNoNew = row[0].inputValue
}

const findData2 = (row: any) => {
  if (row === '保存') {
    if (CategoryGroupDefinitionAddedMethodsParms.value.deptNoNew === '') {
      JMessage.warning('数据输入不完整')
    } else {
      CategoryGroupDefinitionAddedMethods()
    }
  }
}

const CategoryGroupDefinitionAddedMethodsParms = ref<CategoryGroupDefinitionAddedParms>({
  collectDept: '',
  deptName: '',
  deptNo: '',
  deptNoNew: '',
  factoryDept: ''
})

const CategoryGroupDefinitionAddedMethods = async () => {
  const result = await CategoryGroupDefinitionAdded(CategoryGroupDefinitionAddedMethodsParms.value)
  if (result.code === 200) {
    BranchGroupDefinitionMethods()
    dialogVisible1.value = false
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 增加鞋面缴库部门定义
const dialogVisible = ref(false)

const VampDepositAddedMethodsParms = ref<VampDepositAddedParms>({
  collectDept: '',
  collectDeptNew: '',
  collectName: '',
  factoryDept: '',
  groupDept: '',
  persons: '',
  vendorFlag: ''
})

const VampDepositAddedMethods = async () => {
  const result = await VampDepositAdded(VampDepositAddedMethodsParms.value)
  if (result.code === 200) {
    VampToTheWarehouseDepartmentMethods()
    clearFormList2()
    JMessage.success(result.message)
  } else {
    VampToTheWarehouseDepartmentMethods()
    JMessage.error(result.message)
  }
}

// 分厂组别定义删除
const currentRowList1 = (row: any) => {
  const { deptNo } = row
  BranchGroupDefinitionDeletedMethodsParms.value = deptNo
  updateList.value.deptNo = deptNo
}

const BranchGroupDefinitionDeletedMethodsParms = ref('')

const BranchGroupDefinitionDeletedMethods = async () => {
  const reuslt = await BranchGroupDefinitionDeleted(DeleteTheTreasuryDepartmentMethodsParms.value, BranchGroupDefinitionDeletedMethodsParms.value)
  if (reuslt.code === 200) {
    BranchGroupDefinitionMethods()
    BranchGroupDefinitionDeletedMethodsParms.value = ''
    JMessage.success(reuslt.message)
  } else {
    BranchGroupDefinitionMethods()
    BranchGroupDefinitionDeletedMethodsParms.value = ''
    JMessage.error(reuslt.message)
  }
}

const handleCloseMateriaLKlc = (_done?: () => void) => {
  ElMessageBox.confirm('确认要删除数据吗?')
    .then(async () => {
      BranchGroupDefinitionDeletedMethods()
    })
    .catch(() => {})
}

// 鞋面缴库部门定义删除
const DeleteTheTreasuryDepartmentMethodsParms = ref('')

const DeleteTheTreasuryDepartmentMethods = async () => {
  const reuslt = await DeleteTheTreasuryDepartment(DeleteTheTreasuryDepartmentMethodsParms.value)
  if (reuslt.code === 200) {
    VampToTheWarehouseDepartmentMethods()
    BranchGroupDefinitionMethods()
    DeleteTheTreasuryDepartmentMethodsParms.value = ''
    JMessage.success(reuslt.message)
  } else {
    VampToTheWarehouseDepartmentMethods()
    BranchGroupDefinitionMethods()
    DeleteTheTreasuryDepartmentMethodsParms.value = ''
    JMessage.error(reuslt.message)
  }
}

const handleCloseMateriaLKlc5 = (_done?: () => void) => {
  ElMessageBox.confirm('确认要删除数据吗?')
    .then(async () => {
      DeleteTheTreasuryDepartmentMethods()
    })
    .catch(() => {})
}

// 组别
const BranchGroupDefinitionMethodsTableDateList = ref([])

const BranchGroupDefinitionMethodsParms = ref('')

const BranchGroupDefinitionMethods = async () => {
  isLoading1.value = true
  const result = await BranchGroupDefinition(BranchGroupDefinitionMethodsParms.value)
  BranchGroupDefinitionMethodsTableDateList.value = result.map((item: any) => {
    item.change = 'Y'
    return item
  })
  isLoading1.value = false
}

const currentRowList = (row: any) => {
  const { collectDept, factoryDept } = row
  BranchGroupDefinitionMethodsParms.value = collectDept
  DeleteTheTreasuryDepartmentMethodsParms.value = collectDept
  CategoryGroupDefinitionAddedMethodsParms.value.collectDept = collectDept
  CategoryGroupDefinitionAddedMethodsParms.value.factoryDept = factoryDept
  VampToTheWarehouseDepartmentListParms.value = row
  formList5.value[0].selectValue = row.groupDept
  formList5.value[1].inputValue = row.factoryDept
  formList5.value[2].inputValue = row.collectDept
  formList5.value[3].inputValue = row.collectName
  formList5.value[4].inputValue = row.persons
  formList5.value[5].checked = row.vendorFlag
  DepartmentDefinitionModificationParms.value.collectDept = collectDept
  updateList.value.collectDept = collectDept
  updateList.value.factoryDept = factoryDept
  BranchGroupDefinitionMethods()
}

// 鞋面缴库部门
const VampToTheWarehouseDepartmentMethodsTableDateLsit = ref<VampToTheWarehouseDepartmentInterface[]>([])

const VampToTheWarehouseDepartmentMethods = async () => {
  isLoading.value = true
  const result = await VampToTheWarehouseDepartment()
  VampToTheWarehouseDepartmentMethodsTableDateLsit.value = result.map((itme: any) => {
    if (itme.vendorFlag === 'Y') {
      itme.vendorFlag = true
    }
    return itme
  })
  if (result.length > 0) {
    BranchGroupDefinitionMethodsParms.value = result[0].collectDept
    CategoryGroupDefinitionAddedMethodsParms.value.collectDept = result[0].collectDept
    CategoryGroupDefinitionAddedMethodsParms.value.factoryDept = result[0].factoryDept
    updateList.value.collectDept = result[0].collectDept
    updateList.value.factoryDept = result[0].factoryDept
    BranchGroupDefinitionMethods()
  }
  isLoading.value = false
}

onMounted(() => {
  VampToTheWarehouseDepartmentMethods()
})

// 表格数据
const tableHeader = ref([
  {
    label: '部门分类',
    prop: 'groupDept'
  },
  {
    label: '缴库部门',
    prop: 'factoryDept'
  },
  {
    label: '组别代码',
    prop: 'collectDept'
  },
  {
    label: '组别名称',
    prop: 'collectName'
  },
  {
    label: '人数',
    prop: 'persons'
  },
  {
    label: '外包厂商',
    type: 'checkbox',
    prop: 'vendorFlag'
  }
])

const tableHeader1 = ref([
  {
    label: '组别代码',
    prop: 'deptNo'
  },
  {
    label: '组别名称',
    prop: 'deptName'
  }
])
</script>

<style lang="less" scoped>
h1 {
  font-size: 16px;
}
</style>
